using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using MetaBuilders.Irc.Messages;
using MetaBuilders.Irc.Network;
using MetaBuilders.Irc;
using System.Diagnostics;
using System.IO;

namespace ExampleBot
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox ServerEntry;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox ChannelEntry;
		private System.Windows.Forms.Button GoCommand;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Label CurrentStatus;
		private System.Windows.Forms.TextBox ChatEntry;
		private System.Windows.Forms.Button SayIt;
		private System.Windows.Forms.Button SendIt;
		private Button button2;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			String logFolder = Path.Combine( Environment.GetFolderPath( Environment.SpecialFolder.ApplicationData ), @"ExampleBot" );
			String logFile = Path.Combine( logFolder, "Log.txt" );

			Trace.Listeners.Clear();
			DefaultTraceListener defaultListener;
			defaultListener = new DefaultTraceListener();
			Trace.Listeners.Add( defaultListener );
			defaultListener.LogFileName = logFile;

		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if ( disposing )
			{
				if ( components != null )
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.ServerEntry = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.ChannelEntry = new System.Windows.Forms.TextBox();
			this.GoCommand = new System.Windows.Forms.Button();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.CurrentStatus = new System.Windows.Forms.Label();
			this.ChatEntry = new System.Windows.Forms.TextBox();
			this.SayIt = new System.Windows.Forms.Button();
			this.SendIt = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// ServerEntry
			// 
			this.ServerEntry.Anchor = ( (System.Windows.Forms.AnchorStyles)( ( ( System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left )
						| System.Windows.Forms.AnchorStyles.Right ) ) );
			this.ServerEntry.Location = new System.Drawing.Point( 10, 37 );
			this.ServerEntry.Name = "ServerEntry";
			this.ServerEntry.Size = new System.Drawing.Size( 718, 22 );
			this.ServerEntry.TabIndex = 0;
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point( 10, 18 );
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size( 120, 19 );
			this.label1.TabIndex = 1;
			this.label1.Text = "Server";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point( 10, 83 );
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size( 120, 19 );
			this.label2.TabIndex = 3;
			this.label2.Text = "Channel";
			// 
			// ChannelEntry
			// 
			this.ChannelEntry.Anchor = ( (System.Windows.Forms.AnchorStyles)( ( ( System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left )
						| System.Windows.Forms.AnchorStyles.Right ) ) );
			this.ChannelEntry.Location = new System.Drawing.Point( 10, 102 );
			this.ChannelEntry.Name = "ChannelEntry";
			this.ChannelEntry.Size = new System.Drawing.Size( 718, 22 );
			this.ChannelEntry.TabIndex = 2;
			// 
			// GoCommand
			// 
			this.GoCommand.Anchor = ( (System.Windows.Forms.AnchorStyles)( ( System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right ) ) );
			this.GoCommand.Location = new System.Drawing.Point( 594, 148 );
			this.GoCommand.Name = "GoCommand";
			this.GoCommand.Size = new System.Drawing.Size( 134, 26 );
			this.GoCommand.TabIndex = 4;
			this.GoCommand.Text = "Connect And Join";
			this.GoCommand.Click += new System.EventHandler( this.GoCommand_Click );
			// 
			// textBox1
			// 
			this.textBox1.Anchor = ( (System.Windows.Forms.AnchorStyles)( ( ( ( System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom )
						| System.Windows.Forms.AnchorStyles.Left )
						| System.Windows.Forms.AnchorStyles.Right ) ) );
			this.textBox1.Location = new System.Drawing.Point( 10, 185 );
			this.textBox1.Multiline = true;
			this.textBox1.Name = "textBox1";
			this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
			this.textBox1.Size = new System.Drawing.Size( 728, 258 );
			this.textBox1.TabIndex = 5;
			// 
			// button1
			// 
			this.button1.Anchor = ( (System.Windows.Forms.AnchorStyles)( ( System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right ) ) );
			this.button1.Location = new System.Drawing.Point( 484, 148 );
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size( 90, 26 );
			this.button1.TabIndex = 6;
			this.button1.Text = "Disconnect";
			this.button1.Click += new System.EventHandler( this.button1_Click );
			// 
			// CurrentStatus
			// 
			this.CurrentStatus.Location = new System.Drawing.Point( 19, 148 );
			this.CurrentStatus.Name = "CurrentStatus";
			this.CurrentStatus.Size = new System.Drawing.Size( 120, 26 );
			this.CurrentStatus.TabIndex = 7;
			// 
			// ChatEntry
			// 
			this.ChatEntry.Anchor = ( (System.Windows.Forms.AnchorStyles)( ( ( System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left )
						| System.Windows.Forms.AnchorStyles.Right ) ) );
			this.ChatEntry.Location = new System.Drawing.Point( 10, 452 );
			this.ChatEntry.Name = "ChatEntry";
			this.ChatEntry.Size = new System.Drawing.Size( 531, 22 );
			this.ChatEntry.TabIndex = 8;
			// 
			// SayIt
			// 
			this.SayIt.Anchor = ( (System.Windows.Forms.AnchorStyles)( ( System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right ) ) );
			this.SayIt.Location = new System.Drawing.Point( 560, 451 );
			this.SayIt.Name = "SayIt";
			this.SayIt.Size = new System.Drawing.Size( 77, 27 );
			this.SayIt.TabIndex = 9;
			this.SayIt.Text = "Say";
			this.SayIt.Click += new System.EventHandler( this.SayIt_Click );
			// 
			// SendIt
			// 
			this.SendIt.Anchor = ( (System.Windows.Forms.AnchorStyles)( ( System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right ) ) );
			this.SendIt.Location = new System.Drawing.Point( 656, 451 );
			this.SendIt.Name = "SendIt";
			this.SendIt.Size = new System.Drawing.Size( 77, 27 );
			this.SendIt.TabIndex = 10;
			this.SendIt.Text = "Raw";
			this.SendIt.Click += new System.EventHandler( this.SendIt_Click );
			// 
			// button2
			// 
			this.button2.Anchor = ( (System.Windows.Forms.AnchorStyles)( ( System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right ) ) );
			this.button2.Location = new System.Drawing.Point( 337, 148 );
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size( 90, 26 );
			this.button2.TabIndex = 11;
			this.button2.Text = "Log";
			this.button2.Click += new System.EventHandler( this.button2_Click );
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size( 6, 15 );
			this.ClientSize = new System.Drawing.Size( 752, 486 );
			this.Controls.Add( this.button2 );
			this.Controls.Add( this.SendIt );
			this.Controls.Add( this.SayIt );
			this.Controls.Add( this.ChatEntry );
			this.Controls.Add( this.CurrentStatus );
			this.Controls.Add( this.button1 );
			this.Controls.Add( this.textBox1 );
			this.Controls.Add( this.GoCommand );
			this.Controls.Add( this.label2 );
			this.Controls.Add( this.ChannelEntry );
			this.Controls.Add( this.label1 );
			this.Controls.Add( this.ServerEntry );
			this.Name = "Form1";
			this.Text = "Echo Bot";
			this.ResumeLayout( false );
			this.PerformLayout();

		}
		#endregion

		private Client client;


		private void GoCommand_Click( object sender, System.EventArgs e )
		{

			// Create a new client to the given address with the given nick
			client = new Client( this.ServerEntry.Text, "NeboBot" );
			Ident.Service.User = this.client.User;


			// Once I'm welcomed, I can start joining channels
			client.Messages.Welcome += new EventHandler<IrcMessageEventArgs<WelcomeMessage>>( welcomed );

			// People are chatting, pay attention so I can be a lame echobot :)
			client.Messages.Chat += new EventHandler<IrcMessageEventArgs<TextMessage>>( chatting );

			client.Messages.TimeRequest += new EventHandler<IrcMessageEventArgs<TimeRequestMessage>>( timeRequested );

			client.DataReceived += new EventHandler<ConnectionDataEventArgs>( dataGot );
			client.DataSent += new EventHandler<ConnectionDataEventArgs>( dataSent );

			client.Connection.Disconnected += new EventHandler<ConnectionDataEventArgs>( logDisconnected );

			client.EnableAutoIdent = false;

			// Since I'm a Windows.Forms application, i pass in this form to the Connect method so it can sync with me.
			try
			{
				client.Connection.Connect( this );
			}
			catch ( Exception ex )
			{
				MessageBox.Show( ex.Message );
			}

		}

		private void logDisconnected( Object sender, ConnectionDataEventArgs e )
		{
			String data = "*** Disconnected: " + e.Data;
			this.textBox1.Text += data + System.Environment.NewLine;
			this.textBox1.Select( this.textBox1.Text.Length - 1, 1 );
			this.textBox1.ScrollToCaret();
		}

		private void dataGot( Object sender, ConnectionDataEventArgs e )
		{
			String data = "<-" + e.Data;
			this.textBox1.Text += data + System.Environment.NewLine;
			this.textBox1.Select( this.textBox1.Text.Length - 1, 1 );
			this.textBox1.ScrollToCaret();
		}

		private void dataSent( Object sender, ConnectionDataEventArgs e )
		{
			this.textBox1.Text += "->" + e.Data + System.Environment.NewLine;
			this.textBox1.Select( this.textBox1.Text.Length - 1, 1 );
			this.textBox1.ScrollToCaret();
		}

		private void chatting( Object sender, IrcMessageEventArgs<TextMessage> e )
		{
			if ( e.Message.Text.StartsWith( "NeboBot" ) )
			{
				client.SendChat( e.Message.Text, this.ChannelEntry.Text );
			}
		}

		private void timeRequested( Object sender, IrcMessageEventArgs<TimeRequestMessage> e )
		{
			MetaBuilders.Irc.Messages.TimeReplyMessage reply = new MetaBuilders.Irc.Messages.TimeReplyMessage();
			reply.CurrentTime = DateTime.Now.ToLongTimeString();
			reply.Target = e.Message.Sender.Nick;
			client.Send( reply );
		}

		private void welcomed( Object sender, IrcMessageEventArgs<WelcomeMessage> e )
		{
			client.SendJoin( this.ChannelEntry.Text );
		}



		private void button1_Click( object sender, System.EventArgs e )
		{
			if ( client != null )
			{
				client.SendQuit( "I'm Outa Here" );
			}
		}

		private void SayIt_Click( object sender, System.EventArgs e )
		{
			client.SendChat( this.ChatEntry.Text, this.ChannelEntry.Text );
			this.ChatEntry.Text = "";
		}

		private void SendIt_Click( object sender, System.EventArgs e )
		{
			MetaBuilders.Irc.Messages.GenericMessage msg = new MetaBuilders.Irc.Messages.GenericMessage();
			if ( msg.CanParse( this.ChatEntry.Text ) )
			{
				msg.Parse( this.ChatEntry.Text );
				client.Send( msg );
			}
			else
			{
				MessageBox.Show( "Cannot Parse Your Command." );
			}
		}

		private void button2_Click( object sender, EventArgs e )
		{
			System.Diagnostics.Trace.WriteLine( "Stuff" );
		}

	}
}
